<%@ page import="com.walnut.pojo.Food" %>
<%@ page import="java.util.List" %>
<%@ page import="jakarta.servlet.ServletContext" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>食品列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f9f9f9;
        }
        .container {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 10px;
            text-align: center;
        }
        th {
            background-color: #f0f0f0;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #e0e0e0;
        }
        img {
            max-width: 100px;
            height: auto;
            border-radius: 5px;
        }
        .actions a {
            display: inline-block;
            padding: 8px 15px;
            margin: 0 5px;
            color: #FFA500;
            text-decoration: none;
            font-size: 14px;
            cursor: pointer;
            transition: color 0.3s;
        }
        .actions a:hover {
            color: #FF7F00;
        }

        h1 {
            text-align: left;
            margin-bottom: 20px;
        }
        .logout-button {
            display: inline-block;
            padding: 10px 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #d3d3d3;
            color: black;
            font-size: 16px;
            cursor: pointer;
            text-decoration: none;
            transition: background-color 0.3s, color 0.3s;
            float: right;
            margin-bottom: 20px;
        }
        .logout-button:hover {
            background-color: #c0c0c0;
        }

        /* 模态框背景 */
        .modal {
            display: none; /* 默认隐藏 */
            position: fixed; /* 固定定位 */
            z-index: 1; /* 置于顶层 */
            left: 0;
            top: 0;
            width: 100%; /* 全屏宽度 */
            height: 100%; /* 全屏高度 */
            overflow: auto; /* 溢出滚动 */
            background-color: rgba(0,0,0,0.4); /* 半透明背景 */
        }

        /* 模态框内容 */
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto; /* 距离顶部15%，水平居中 */
            padding: 20px;
            border: 1px solid #888;
            width: 30%; /* 宽度30% */
            max-width: 400px; /* 最大宽度400px */
            text-align: center;
        }

        /* 关闭按钮 */
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }

        /* 模态框按钮 */
        .modal-buttons button {
            padding: 10px 20px;
            margin: 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        #confirmPurchaseBtn {
            background-color: #dc3545;
            color: white;
        }

        #confirmPurchaseBtn:hover {
            background-color: #c82333;
        }

        #cancelPurchaseBtn {
            background-color: #6c757d;
            color: white;
        }

        #cancelPurchaseBtn:hover {
            background-color: #5a6268;
        }

        /* 查询容器，居中对齐 */
        .search-container {
            display: flex;
            justify-content: center; /* 水平居中 */
            align-items: center;     /* 垂直居中 */
            gap: 15px;
            margin: 20px 0;
        }

        /* 查询输入框的统一样式 */
        .search-container input, .search-container select {
            padding: 10px;
            font-size: 14px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #fff;
            color: #333;
            width: 250px;  /* 设置宽度为250px，保证布局合理 */
            box-sizing: border-box;
            transition: all 0.3s ease; /* 平滑过渡效果 */
        }

        /* 输入框聚焦时的效果 */
        .search-container input:focus, .search-container select:focus {
            border-color: #007BFF;  /* 聚焦时修改边框颜色 */
            outline: none;  /* 去除默认的outline */
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.3); /* 聚焦时添加阴影 */
        }

        /* 查询按钮样式 */
        .search-container button {
            background-color: #007BFF;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s ease;
            font-size: 14px;
        }

        .search-container button:hover {
            background-color: #0056b3;
        }

        /* 响应式布局：调整小屏幕时的输入框布局 */
        @media (max-width: 768px) {
            .search-container {
                flex-direction: column;
                align-items: stretch;
            }

            .search-container input, .search-container select, .search-container button {
                width: 100%;  /* 在小屏幕下宽度为100% */
                margin-bottom: 10px;
            }
        }

        .base-button {
            display: inline-block;
            padding: 10px 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #d3d3d3;
            color: black;
            font-size: 16px;
            cursor: pointer;
            text-decoration: none;
            transition: background-color 0.3s, color 0.3s;
        }
        .base-button:hover {
            background-color: #c0c0c0;
        }
    </style>
</head>
<body>

<div class="search-container">
    <input type="text" id="searchFoodName" placeholder="请输入食品名称..." />
    <input type="text" id="searchPrice" placeholder="请输入食品价格..." />
    <select id="searchFoodStatus">
        <option value="">无</option>
        <option value="起售">起售</option>
        <option value="停售">停售</option>
    </select>
    <button class="base-button" onclick="searchFood()">查询</button>
</div>

<div class="container">
    <h1>食品列表</h1>

    <!-- 退出登录按钮 -->
    <a class="logout-button" onclick="confirmLogout()">退出登录</a>

    <!-- 查看订单按钮 -->
    <a class="base-button" href="userListOrder.jsp">查看订单</a>

    <!-- 表格头部 -->
    <table>
        <thead>
        <tr>
            <th>序号</th>
            <th>食品图片</th>
            <th>食品名称</th>
            <th>价格</th>
            <th>售卖状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%
            ServletContext context=request.getServletContext();

            // 从 ServletContext 中获取食品列表
            List<Food> foods = (List<Food>) context.getAttribute("foods");
            if (foods != null && !foods.isEmpty()) {
                int index = 1; // 序号从1开始
                for (Food food : foods) {
        %>
        <tr>
            <td><%= index %></td>
            <td><img src="<%= food.getFoodImg() %>"></td>
            <td><%= food.getFoodName() %></td>
            <td><%= food.getPrice() %></td>
            <td><%= food.getFoodStatus() %></td>
            <td class="actions">
                <a href="javascript:void(0);" class="purchase" onclick="confirmPurchase('<%= food.getFoodId() %>')">购买</a>
            </td>
        </tr>
        <%
                index++; // 序号递增
            }
        } else {
        %>
        <tr>
            <td colspan="6">没有找到相关食品。</td>
        </tr>
        <%
            }
        %>
        </tbody>
    </table>
</div>

<!-- 模态框 -->
<div id="purchaseModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <p>是否购买？</p>
        <div class="modal-buttons">
            <button id="confirmPurchaseBtn">确认</button>
            <button id="cancelPurchaseBtn">取消</button>
        </div>
    </div>
</div>

<!-- 退出登录模态框 -->
<div id="logoutModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <p>是否退出登录？</p>
        <div class="modal-buttons">
            <button id="confirmLogoutBtn">确认</button>
            <button id="cancelLogoutBtn">取消</button>
        </div>
    </div>
</div>
<script>
    var purchaseModal = document.getElementById("purchaseModal");
    var span = document.getElementsByClassName("close")[0];
    var confirmPurchaseBtn = document.getElementById("confirmPurchaseBtn");
    var cancelPurchaseBtn = document.getElementById("cancelPurchaseBtn");

    var logoutModal = document.getElementById("logoutModal");
    var logoutSpan = document.getElementsByClassName("close")[1];
    var confirmLogoutBtn = document.getElementById("confirmLogoutBtn");
    var cancelLogoutBtn = document.getElementById("cancelLogoutBtn");

    // 打开模态框
    function confirmPurchase(foodId) {
        document.getElementById("confirmPurchaseBtn").setAttribute("data-food-id", foodId);
        purchaseModal.style.display = "block";
    }
    // 关闭模态框
    span.onclick = function() {
        purchaseModal.style.display = "none";
    }
    // 点击确认按钮
    confirmPurchaseBtn.onclick = function() {
        var foodId = this.getAttribute("data-food-id");
        // 使用 AJAX 发送异步请求
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/addOrder?goodsTypeId=' + foodId, true);
        // 关闭模态框
        purchaseModal.style.display = "none";
        xhr.send();
        Swal.fire({
            title: '<span style="font-size: 20px">购买成功</span>',
            width: '20%',
            height: '25%',
            showConfirmButton: false,
            timer: 1500,
        })
    }
    // 点击取消按钮
    cancelPurchaseBtn.onclick = function() {
        purchaseModal.style.display = "none";
    }
    // 点击模态框外部关闭
    window.onclick = function(event) {
        if (event.target == modal) {
            purchaseModal.style.display = "none";
        }
    }


    // 打开模态框
    function confirmLogout() {
        logoutModal.style.display = "block";
    }
    // 关闭模态框
    logoutSpan.onclick = function() {
        logoutModal.style.display = "none";
    }
    // 点击确认按钮
    confirmLogoutBtn.onclick = function() {
        // 发送请求到后端，通知服务器删除 loginer 字段
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/logout', true); // 假设你创建了一个 "/logout" 的后端请求
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                // 成功移除 loginer 后，跳转到 login.jsp
                window.location.href = "login.jsp";
                logoutModal.style.display = "none";
            }
        };
        xhr.send();
    }
    // 点击取消按钮
    cancelLogoutBtn.onclick = function() {
        logoutModal.style.display = "none";
    }
    // 点击模态框外部关闭
    window.onclick = function(event) {
        if (event.target == logoutModal) {
            logoutModal.style.display = "none";
        }
    }


    // 搜索功能
    function searchFood() {
        var foodName = document.getElementById("searchFoodName").value.trim();
        var price = document.getElementById("searchPrice").value.trim();
        var foodStatus = document.getElementById("searchFoodStatus").value.trim();

        // 构建查询参数
        var queryParams = [];
        if (foodName) queryParams.push("foodName=" + encodeURIComponent(foodName));
        if (price) queryParams.push("price=" + encodeURIComponent(price));
        if (foodStatus) queryParams.push("foodStatus=" + encodeURIComponent(foodStatus));

        var queryString = queryParams.length > 0 ? "?" + queryParams.join("&") : "";

        // 使用 AJAX 请求后端进行查询
        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/selectFood' + queryString, true);
        xhr.onload = function() {
            if (xhr.status === 200) {
                // 直接插入返回的 HTML
                document.querySelector("table tbody").innerHTML = xhr.responseText;
            }
        };
        xhr.send();
    }
</script>
</body>
</html>